<template>
  <div v-if="showSplash" class="splash-container">
    <!-- 跳过按钮 -->
    <div class="skip-btn" @click="skipAnimation">
      跳过 {{ countdown }}s
    </div>
    
    <!-- Canvas动画容器 -->
    <canvas ref="canvas" class="splash-canvas"></canvas>
    
    <!-- 动画内容 -->
    <div class="splash-content">
      <div class="logo-text">
        <h1>大麦网</h1>
        <p>Damai.cn</p>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'SplashAnimation',
  setup() {
    const router = useRouter()
    const canvas = ref(null)
    const showSplash = ref(true)
    const countdown = ref(5) // 5秒后自动跳过
    let animationId = null
    let countdownInterval = null
    let ctx = null

    // Canvas粒子动画
    const initCanvas = () => {
      const canvasEl = canvas.value
      ctx = canvasEl.getContext('2d')
      
      // 设置Canvas尺寸
      canvasEl.width = window.innerWidth
      canvasEl.height = window.innerHeight

      // 粒子系统
      const particles = []
      const particleCount = 100

      // 创建粒子
      for (let i = 0; i < particleCount; i++) {
        particles.push({
          x: Math.random() * canvasEl.width,
          y: Math.random() * canvasEl.height,
          size: Math.random() * 3 + 1,
          speedX: Math.random() * 2 - 1,
          speedY: Math.random() * 2 - 1,
          color: `hsl(${Math.random() * 60 + 330}, 70%, 60%)` // 粉色系
        })
      }

      // 动画循环
      const animate = () => {
        ctx.clearRect(0, 0, canvasEl.width, canvasEl.height)
        
        // 更新和绘制粒子
        particles.forEach(particle => {
          particle.x += particle.speedX
          particle.y += particle.speedY

          // 边界检测
          if (particle.x < 0 || particle.x > canvasEl.width) particle.speedX *= -1
          if (particle.y < 0 || particle.y > canvasEl.height) particle.speedY *= -1

          // 绘制粒子
          ctx.beginPath()
          ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2)
          ctx.fillStyle = particle.color
          ctx.fill()
        })

        // 绘制中心光效
        const gradient = ctx.createRadialGradient(
          canvasEl.width / 2, canvasEl.height / 2, 0,
          canvasEl.width / 2, canvasEl.height / 2, 200
        )
        gradient.addColorStop(0, 'rgba(255, 45, 121, 0.3)')
        gradient.addColorStop(1, 'rgba(255, 45, 121, 0)')
        
        ctx.beginPath()
        ctx.arc(canvasEl.width / 2, canvasEl.height / 2, 200, 0, Math.PI * 2)
        ctx.fillStyle = gradient
        ctx.fill()

        animationId = requestAnimationFrame(animate)
      }

      animate()
    }

    // 跳过动画
    const skipAnimation = () => {
      showSplash.value = false
      router.replace('/login')
      clearAnimations()
    }

    // 清理动画
    const clearAnimations = () => {
      if (animationId) {
        cancelAnimationFrame(animationId)
      }
      if (countdownInterval) {
        clearInterval(countdownInterval)
      }
    }

    // 倒计时自动跳过
    const startCountdown = () => {
      countdownInterval = setInterval(() => {
        countdown.value--
        if (countdown.value <= 0) {
          skipAnimation()
        }
      }, 1000)
    }

    onMounted(() => {
      // 检查是否已经看过启动页
      const hasSeenSplash = localStorage.getItem('hasSeenSplash')
      
      if (hasSeenSplash) {
        // 已经看过，直接跳转到登录页
        router.replace('/login')
      } else {
        // 第一次看，显示启动动画
        localStorage.setItem('hasSeenSplash', 'true')
        initCanvas()
        startCountdown()
        
        // 监听窗口大小变化
        window.addEventListener('resize', initCanvas)
      }
    })

    onUnmounted(() => {
      clearAnimations()
      window.removeEventListener('resize', initCanvas)
    })

    return {
      canvas,
      showSplash,
      countdown,
      skipAnimation
    }
  }
}
</script>

<style scoped>
.splash-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #0c0c0c, #1a1a2e, #16213e);
  z-index: 9999;
  overflow: hidden;
}

.splash-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.skip-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
  z-index: 100;
}

.skip-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
}

.splash-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: white;
  z-index: 10;
}

.logo-text h1 {
  font-size: 60px;
  margin: 0 0 10px 0;
  font-weight: bold;
  background: linear-gradient(45deg, #ff2d79, #ff6b6b, #ffa726);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 0 30px rgba(255, 45, 121, 0.5);
  animation: glow 2s ease-in-out infinite alternate;
}

.logo-text p {
  font-size: 18px;
  margin: 0;
  opacity: 0.8;
  letter-spacing: 2px;
}

@keyframes glow {
  from {
    text-shadow: 0 0 20px rgba(255, 45, 121, 0.5),
                 0 0 30px rgba(255, 45, 121, 0.3);
  }
  to {
    text-shadow: 0 0 30px rgba(255, 45, 121, 0.8),
                 0 0 40px rgba(255, 45, 121, 0.5),
                 0 0 50px rgba(255, 45, 121, 0.3);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .logo-text h1 {
    font-size: 40px;
  }
  
  .logo-text p {
    font-size: 14px;
  }
  
  .skip-btn {
    top: 10px;
    right: 10px;
    padding: 6px 12px;
    font-size: 12px;
  }
}
</style>